package com.jolink.omiis.wechat.faq;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonObject;
import com.jolink.omiis.wechat.SpringContextUtil;
import com.jolink.omiis.wechat.WeChatContext;
import com.jolink.omiis.wechat.keys.IPreferences;
import com.jolink.omiis.wechat.redis.IRedisProcessor;
import com.jolink.omiis.wechat.util.GsonUtil;

/**
 * @author WangYanqing
 * 提示语获取
 *
 */
public class ExpressionConfiguration {

	@Autowired
	private WeChatContext context;

	private static Logger logger = LoggerFactory.getLogger(ExpressionConfiguration.class);

	public String getOvertimeConfig(String type) {
		Map<String, String> paramConfig = null;
		try {
			paramConfig = context.getServiceUtil().getRedisProcessor().hgetall(IPreferences.OM_PARAM_CONFIG);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		if (paramConfig == null || paramConfig.isEmpty()) {
			return null;
		}
		String jsonString = paramConfig.get(type);
		Map<String, String> jsonMap = GsonUtil.str2Map(jsonString);
		String param_describe = jsonMap.get("param_describe");
		logger.info("获取超时提示语配置，type：{}，value：{}。", type, param_describe);
		return param_describe;
	}

	/**
	 * @return
	 */
	// TODO
	public String getAgentTimeoutTip2() {
		String tip = getOvertimeConfig("agentTimeoutTip2");
		if (StringUtils.isEmpty(tip)) {
			tip = "当前坐席繁忙，很遗憾的与您结束本次通话，您可稍候重新申请人工。";
		}
		return tip;
	}

	public String getThankTip() {
		return getOvertimeConfig("thankTip");
	}

	public String getRobotEndWords() {
		return "机器人无法理解您的意思，如需人工，请回复序号0。";
	}

	public String getRobotEndWords2() {
		return "如需人工，请回复序号0。";
	}

	public String getCustomerTimeout1() {
		return getOvertimeConfig("customerTimeout1");
	}

	public String getCustomerTimeout2() {
		return getOvertimeConfig("customerTimeout2");
	}

	public String getTransferAgentTip() {
		return getOvertimeConfig("transferAgentTip");
	}

	public String getTransferAgentTip2() {
		return getOvertimeConfig("transferAgentTip2");
	}

	public String getQueueTimeoutTip() {
		return getOvertimeConfig("queueTimeoutTip");
	}

	public String getQueueTimeoutTip2() {
		String tip = getOvertimeConfig("queueTimeoutTip2");
		if (StringUtils.isEmpty(tip)) {
			tip = "尊敬的顾客：很抱歉，人工客服繁忙，您可通过拨打服务电话进行咨询。";
		}
		return tip;
	}

	public String getAgentTimeoutTip() {
		return getOvertimeConfig("agentTimeoutTip");
	}

	public String getEventInterveneTip(String agentId) {
		String tip = getOvertimeConfig("eventInterveneTip");
		tip = tip.replaceAll("\\{[^}]*\\}", agentId);
		return tip;
	}

	/**
	 * 待配置
	 *
	 * @param agentId
	 * @return
	 */
	// TODO
	public String getEventConferenceTip(String agentId) {
		String tip = getOvertimeConfig("eventConferenceTip");
		if (StringUtils.isEmpty(tip)) {
			tip = "客服：{agentId}加入会话。";
		}
		tip = tip.replaceAll("\\{[^}]*\\}", agentId);
		return tip;
	}

	public String getTakeoverTip(String agentId) {
		String tip = getOvertimeConfig("takeoverTip");
		tip = tip.replaceAll("\\{[^}]*\\}", agentId);
		return tip;
	}

	public String getAgentDisconnectTip() {
		return getOvertimeConfig("agentDisconnectTip");
	}

	public String getHangupTip() {
		return getOvertimeConfig("hangupTip");
	}

	public String getEventTalkTip(String agentId) {
		String tip = getOvertimeConfig("eventTalkTip");
		tip = tip.replaceAll("\\{[^}]*\\}", agentId);
		return tip;
	}

	public String getSatisfactionSurveyMessage() {
		return getOvertimeConfig("satisfactionSurveyMessage");
	}

	/**
	 * 微信用户关注欢迎语
	 * 
	 * @return
	 */
	public String getWelcome() {
		String tip = getOvertimeConfig("welcome");
		if (StringUtils.isEmpty(tip)) {
			tip = getWelcome2();
		}
		return tip;
	}

	/**
	 * 目前欢迎语配置在全媒体管理端库，读取无法正常显示
	 * 
	 * @return
	 */
	public static String getWelcome2() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("亲，感谢您关注58钱柜！").append("\n");
		buffer.append("                    ").append("\n");
		buffer.append("注册58钱柜 <a href=\"http://58.com\">领取28888体验金</a>").append("\n");
		buffer.append("兑换码换红包请 <a href=\"https://qiangui.58.com/help/activ150723\">点此兑换</a>").append("\n");
		buffer.append("                    ").append("\n");
		buffer.append("58钱柜是58同城旗下理财平台，年利率5.5%-10.5%").append("\n");
		buffer.append("                    ").append("\n");
		buffer.append("                    ").append("\n");
		buffer.append("理财QQ群：515318526").append("\n");
		buffer.append("客服电话：4008685858").append("\n");
		buffer.append("                    ").append("\n");
		buffer.append("了解58具体详情，请点击").append("\n");
		buffer.append("【介绍】<a href=\"http://qiangui.58.com/help/activ150507?qq-pf-to=pcqq.c2c\">58钱柜简介</a>")
				.append("\n");
		buffer.append("【问题】<a href=\"https://qiangui.58.com/help/help-center\">常见问题解答</a>").append("\n");
		buffer.append("【攻略】<a href=\"https://qiangui.58.com/active/activ150721\">新手理财引导</a>").append("\n");
		buffer.append("更多粉丝福利活动请点击菜单“福利”").append("\n");
		return buffer.toString();
	}

	public String getRestTimeTip() {
		return getOmMessageTip(IPreferences.REST_TIME_TIP);
	}

	private String getOmMessageTip(String type) {
		IRedisProcessor redisProcessor = SpringContextUtil.getBean(IRedisProcessor.class);
		Map<String, String> row = null;
		String content = null;
		try {
			row = redisProcessor.hgetall(IPreferences.OM_MESSAGE_TIP);
			String jsonStr = row.get(type);
			if (StringUtils.isEmpty(jsonStr)) {
				throw new RuntimeException("OM_PARAM_CONFIG未配置：" + type + "。");
			}
			JsonObject config = GsonUtil.json2JsonObject(jsonStr);
			content = config.get("value").getAsString();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("从Redis获取欢迎语配置信息：{}。", content);
		return content;
	}

	/**
	 * 微信用户接入坐席提示语
	 * 
	 * @return
	 */
	@Deprecated
	public String getGreeting() {
		return getOmMessageTip("greeting");
	}

	@Deprecated
	public static String createFAQList() {
		StringBuffer faqBuffer = new StringBuffer();
		faqBuffer.append("咨询问题，请回复序号：").append("\n");
		faqBuffer.append("1、我要借款").append("\n");
		faqBuffer.append("2、借款进度").append("\n");
		faqBuffer.append("3、提现相关").append("\n");
		faqBuffer.append("4、充值相关").append("\n");
		faqBuffer.append("5、还款相关").append("\n");
		faqBuffer.append("6、账户相关").append("\n");
		faqBuffer.append("7、我要投资").append("\n");
		faqBuffer.append("8、人工客服");
		return faqBuffer.toString();
	}

	@Deprecated
	public static String createDefaultFAQ() {
		StringBuffer response = new StringBuffer();
		response.append("你好，小卓没理解您的意思，建议您换个问法再试试。您也可以回复序号，了解相关信息：").append("\n");
		response.append(createFAQList());
		return response.toString();
	}
}
